<!--- 

kalendarCategories CFC

COPYRIGHT & LICENSING INFO
-------------------------------------------------------------------

Copyright 2006 TJ Downes - tdownes@sanative.net - http://www.sanative.net

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
--->

<cfcomponent displayname="kalendarCategories"
		hint="This CFC is intended to manage categories for the Kalendar application."
		output="no">

	<cfset variables.instance = structNew() />
	
	<cffunction name="init" output="false" access="public" returntype="KalendarCategories" hint="I initialize this component">
		<cfargument name="DSN" type="string" required="true" hint="This is the name of the datasource in the CF Administrator for Kalendar"/>
		<cfargument name="DBType" type="string" required="true" hint="This is the Database Platform used for Kalendar. Possible Values:  MSSQL, MYSQL"/>
		<cfargument name="KalendarConfig" type="any" required="true" hint="This is a reference to the main Kalendar Config component"/>

		<cfset variables.instance.dsn = arguments.dsn />
		<cfset variables.instance.DBType = arguments.DBType />
		<cfset variables.instance.KalendarConfig = arguments.KalendarConfig />

		<cfreturn this />

	</cffunction>	

	<!--- Function getSingleRecord ---->
	<cffunction access="public" 
			name="getSingleRecord" 
			output="false" 
			returntype="query" 
			displayname="getSingleRecord" 
			hint="Retrieves a single database record based upon the ID sent. Return output contained in queryDetails variable, of type query.">
			
		<cfargument name="EventCategoryID" 
				type="string" 
				required="true" 
				default="" 
				displayname="EventCategoryID" 
				hint="The ID of the item to be retrieved. This is the same as the database table fieldname." />
		
		<cfset var queryDetails = "" />
		
		<!--- Query table for a single record. --->
		<cfquery name="queryDetails" datasource="#getDSN()#">
			
			SELECT
				*
			FROM
				tblKalendarCategories
			WHERE
				CategoryID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.CategoryID#" />
		
		</cfquery>
		
		<cfreturn queryDetails />
	
	</cffunction>

	<!--- Function createRecord --->
	<cffunction access="public" 
			name="createRecord" 
			output="false" 
			returntype="query" 
			displayname="createRecord" 
			hint="Adds a new database record. The return output is returnedMessage variable, a string message indicating success or failure.">
			
		<cfargument name="CategoryName" 
				type="string" 
				required="yes"
				displayname="CategoryName" 
				hint="The name of the item." />	
					
		<cfargument name="CategoryBGColor" 
				type="string" 
				required="yes"
				default=""
				displayname="CategoryBGColor" 
				hint="The bg color of the item." />
				
		<cfargument name="CategoryTextColor" 
				type="string" 
				required="yes"
				default=""
				displayname="CategoryTextColor" 
				hint="The text color of the item." />
				
		<cftransaction>
			
		<!--- Add new record to table.--->
		<cfquery name="createRecord" datasource="#getDSN()#">
		
			INSERT INTO
				tblKalendarCategories
				(
					CategoryName,
					CategoryBGColor,
					CategoryTextColor
				)
			VALUES
				(
					<cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(arguments.CategoryName)#" />,
					<cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(arguments.CategoryBGColor)#" null="#NOT Len(Trim(CategoryBGColor))#"/>,
					<cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(arguments.CategoryTextColor)#" null="#NOT Len(Trim(CategoryTextColor))#"/>
				)
			
			<cfswitch expression="#getDBType()#">
				<cfcase value="MSSQL">
			SELECT
				@@IDENTITY AS NewCategoryID
				</cfcase>
			</cfswitch>
				
		</cfquery>
		
		<cfswitch expression="#getDBType()#">
		<cfcase value="MySQL">
			<cfquery name="NewCategoryID" datasource="#getDSN()#">
				SELECT
					DISTINCT LAST_INSERT_ID() AS NewCategoryID
				FROM
					tblKalendarCategories
			</cfquery>
			<cfreturn NewCategoryID />
		</cfcase>
		<cfcase value="MSSQL">
			<cfreturn createRecord />
		</cfcase>
		</cfswitch>
		
		</cftransaction>
		
	</cffunction>

	<!--- Function updateRecord --->
	<cffunction access="public" 
			name="updateRecord" 
			output="false" 
			returntype="string" 
			displayname="updateRecord" 
			hint="Updates a single database record based upon the ID sent. The return output is returnMessage variable, a string message indicating success or failure.">

			
		<cfargument name="CategoryID" 
				type="numeric" 
				required="yes"
				displayname="CategoryID" 
				hint="The category id." />
			
		<cfargument name="CategoryName" 
				type="string" 
				required="yes"
				displayname="CategoryName" 
				hint="The name of the item." />
					
		<cfargument name="CategoryBGColor" 
				type="string" 
				required="yes"
				default=""
				displayname="CategoryBGColor" 
				hint="The name of the item." />
				
		<cfargument name="CategoryTextColor" 
				type="string" 
				required="yes"
				default=""
				displayname="CategoryTextColor" 
				hint="The text color of the item." />
			
		<cfset var returnMessage = "Record has been updated." />
			
		<cfquery name="updateRecord" datasource="#getDSN()#">
		
			UPDATE
				tblKalendarCategories
			SET
				CategoryName = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(arguments.CategoryName)#" />,
				CategoryBGColor = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(arguments.CategoryBGColor)#" null="#NOT Len(Trim(CategoryBGColor))#"/>,
				CategoryTextColor = <cfqueryparam cfsqltype="cf_sql_varchar" value="#Trim(arguments.CategoryTextColor)#" null="#NOT Len(Trim(CategoryTextColor))#"/>
				
			WHERE
				CategoryID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.CategoryID#" />
			
		</cfquery>
		
		<cfreturn returnMessage />
				
	</cffunction>
	
	<!--- Function deleteRecord --->
	<cffunction access="public" 
			name="deleteRecord" 
			output="false" 
			returntype="string" 
			displayname="deleteRecord" 
			hint="deletes a single database record based upon the ID sent if there are no child records. The return output is returnMessage variable, a string message indicating success or failure.">
			
		<cfargument name="CategoryID" 
				type="numeric" 
				required="yes"
				displayname="CategoryID" 
				hint="The category id." />
		
		<cfset var returnMessage = "" />
			
		<cfquery name="getChildRecords" datasource="#getDSN()#">
			
			SELECT
				EventID
			FROM
				tblKalendar
			WHERE
				CategoryID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.CategoryID#" />
				
		</cfquery>
		
		<cfif NOT getChildRecords.RecordCount>
		
			<cfquery name="deleteRecord" datasource="#getDSN()#">
			
				DELETE FROM
					tblKalendarCategories
				WHERE
					CategoryID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.CategoryID#" />
				
			</cfquery>
		
		
			<cfset returnMessage = "Record has been deleted." />
		
		<cfelse>
		
			<cfset returnMessage = "Child records exist. This category canot be deleted." />
		
		</cfif>
		
		<cfreturn returnMessage />
				
	</cffunction>

	<!--- Function getMultipleRecords --->
	<cffunction access="public" 
			name="getMultipleRecords" 
			output="false" 
			returntype="query" 
			displayname="getMultipleRecords" 
			hint="Gets all categories">
				
		<cfset var getMultipleRecords = "" />
		
		<cfquery name="getMultipleRecords" datasource="#getDSN()#">
		
			SELECT
				*
			FROM
				tblKalendarCategories
			ORDER BY
				CategoryName
			
		</cfquery>
		
		<cfreturn getMultipleRecords />
				
	</cffunction>

	<cffunction name="getDSN" access="private" output="false" returntype="string">
		<cfreturn variables.instance.DSN/>
	</cffunction>	
	
	<cffunction name="getDBType" access="private" output="false" returntype="string">
		<cfreturn variables.instance.DBType/>
	</cffunction>
	
	<cffunction name="getKalendarConfig" access="private" output="false" returntype="any">
		<cfreturn variables.instance.KalendarConfig/>
	</cffunction>
		
		
</cfcomponent>